package levels;

import java.util.ArrayList;

import lobster.games.studios.R;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class Level6 extends Activity {

	private Button[] buttonsPlus;
	private Button[] buttonsLess;
	private TextView[] textValues;
	private TextView[] textFlushs;
	private ImageView cardOne, cardTwo, cardThree;
	private ArrayList<String> flush = new ArrayList<String>();// = {"\u2660",
	// "\u2663",
	// "\u2666",
	// "\u2665"};
	private ArrayList<String> value = new ArrayList<String>(); // = {"2", "3",
	// "4", "5",
	// "6", "7",
	// "8", "9",
	// "10", "J",
	// "Q", "K",
	// "A"};
	private int i, aux;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.level6);
		getWindow().addFlags(LayoutParams.FLAG_KEEP_SCREEN_ON);

		cardOne = new ImageView(this);
		cardTwo = new ImageView(this);
		cardThree = new ImageView(this);

		cardOne = (ImageView) findViewById(R.id.cardOne);
		cardTwo = (ImageView) findViewById(R.id.cardTwo);
		cardThree = (ImageView) findViewById(R.id.cardThree);
		// cardOne.setImageResource(R.drawable.espadas5);

		flush.add("\u2660");
		flush.add("\u2663");
		flush.add("\u2666");
		flush.add("\u2665");

		value.add("2");
		value.add("3");
		value.add("4");
		value.add("5");
		value.add("6");
		value.add("7");
		value.add("8");
		value.add("9");
		value.add("10");
		value.add("J");
		value.add("Q");
		value.add("K");
		value.add("A");

		buttonsPlus = findPlusButtons();
		buttonsLess = findLessButtons();
		textValues = findTextValueViews();
		textFlushs = findTextFlushViews();

		textFlushs[0].setText("\u2660");
		textFlushs[0].setTextColor(Color.BLACK);
		textFlushs[0].setTextSize(30.0f);
		textFlushs[1].setText("\u2665");
		textFlushs[1].setTextColor(Color.RED);
		textFlushs[1].setTextSize(30.0f);
		textFlushs[2].setText("\u2663");
		textFlushs[2].setTextColor(Color.BLACK);
		textFlushs[2].setTextSize(30.0f);

		textValues[0].setText("2");
		textValues[0].setTextSize(30.0f);
		textValues[0].setTextColor(Color.BLACK);
		textValues[1].setText("K");
		textValues[1].setTextSize(30.0f);
		textValues[1].setTextColor(Color.BLACK);
		textValues[2].setText("7");
		textValues[2].setTextSize(30.0f);
		textValues[2].setTextColor(Color.BLACK);

		for (i = 0; i < 6; i++) {
			buttonsPlus[i].setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					changeValue(((Button) v).getId());
				}
			});
		}
		for (i = 0; i < 6; i++) {

			buttonsLess[i].setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					changeValue(((Button) v).getId());
				}
			});
		}
	}

	public Button[] findPlusButtons() {
		Button[] b = new Button[6];
		for (int a = 0; a < b.length; a++) {
			b[a] = new Button(this);
		}

		b[0] = (Button) findViewById(R.id.buttonPlus11);
		b[1] = (Button) findViewById(R.id.buttonPlus12);
		b[2] = (Button) findViewById(R.id.buttonPlus21);
		b[3] = (Button) findViewById(R.id.buttonPlus22);
		b[4] = (Button) findViewById(R.id.buttonPlus31);
		b[5] = (Button) findViewById(R.id.buttonPlus32);

		return b;
	}

	public Button[] findLessButtons() {
		Button[] b = new Button[6];
		for (int a = 0; a < b.length; a++) {
			b[a] = new Button(this);
		}

		b[0] = (Button) findViewById(R.id.buttonLess11);
		b[1] = (Button) findViewById(R.id.buttonLess12);
		b[2] = (Button) findViewById(R.id.buttonLess21);
		b[3] = (Button) findViewById(R.id.buttonLess22);
		b[4] = (Button) findViewById(R.id.buttonLess31);
		b[5] = (Button) findViewById(R.id.buttonLess32);

		return b;
	}

	public TextView[] findTextFlushViews() {
		TextView[] b = new TextView[3];
		for (int a = 0; a < b.length; a++) {
			b[a] = new TextView(this);
		}

		b[0] = (TextView) findViewById(R.id.text12);
		b[1] = (TextView) findViewById(R.id.text22);
		b[2] = (TextView) findViewById(R.id.text32);

		return b;
	}

	public TextView[] findTextValueViews() {
		TextView[] b = new TextView[3];
		for (int a = 0; a < b.length; a++) {
			b[a] = new TextView(this);
		}

		b[0] = (TextView) findViewById(R.id.text11);
		b[1] = (TextView) findViewById(R.id.text21);
		b[2] = (TextView) findViewById(R.id.text31);

		return b;
	}

	public void changeValue(int id) {
		switch (id) {
		case (R.id.buttonPlus11):
			aux = value.indexOf(textValues[0].getText().toString()) + 1;
			if (aux == value.size())
				aux = 0;
			textValues[0].setText(value.get(aux));
			break;
		case (R.id.buttonPlus12):
			aux = flush.indexOf(textFlushs[0].getText().toString()) + 1;
			if (aux == flush.size())
				aux = 0;
			if (aux > 1) {
				textFlushs[0].setTextColor(Color.RED);
			} else {
				textFlushs[0].setTextColor(Color.BLACK);
			}
			textFlushs[0].setText(flush.get(aux));
			break;
		case (R.id.buttonPlus21):
			aux = value.indexOf(textValues[1].getText().toString()) + 1;
			if (aux == value.size())
				aux = 0;
			System.out.println("" + value.get(aux));
			textValues[1].setText(value.get(aux));
			break;
		case (R.id.buttonPlus22):
			aux = flush.indexOf(textFlushs[1].getText().toString()) + 1;
			if (aux == flush.size())
				aux = 0;
			if (aux > 1) {
				textFlushs[1].setTextColor(Color.RED);
			} else {
				textFlushs[1].setTextColor(Color.BLACK);
			}
			textFlushs[1].setText(flush.get(aux));
			break;
		case (R.id.buttonPlus31):
			aux = value.indexOf(textValues[2].getText().toString()) + 1;
			if (aux == value.size())
				aux = 0;
			textValues[2].setText(value.get(aux));
			break;
		case (R.id.buttonPlus32):
			aux = flush.indexOf(textFlushs[2].getText().toString()) + 1;
			if (aux == flush.size())
				aux = 0;
			if (aux > 1) {
				textFlushs[2].setTextColor(Color.RED);
			} else {
				textFlushs[2].setTextColor(Color.BLACK);
			}
			textFlushs[2].setText(flush.get(aux));
			break;
		case (R.id.buttonLess11):
			aux = value.indexOf(textValues[0].getText().toString()) - 1;
			if (aux == -1)
				aux = value.size() - 1;
			textValues[0].setText(value.get(aux));
			break;

		case (R.id.buttonLess12):
			aux = flush.indexOf(textFlushs[0].getText().toString()) - 1;
			if (aux == -1)
				aux = flush.size() - 1;
			if (aux > 1) {
				textFlushs[0].setTextColor(Color.RED);
			} else {
				textFlushs[0].setTextColor(Color.BLACK);
			}
			textFlushs[0].setText(flush.get(aux));
			break;
		case (R.id.buttonLess21):
			aux = value.indexOf(textValues[1].getText().toString()) - 1;
			if (aux == -1)
				aux = value.size() - 1;
			textValues[1].setText(value.get(aux));
			break;
		case (R.id.buttonLess22):
			aux = flush.indexOf(textFlushs[1].getText().toString()) - 1;
			if (aux == -1)
				aux = flush.size() - 1;
			if (aux > 1) {
				textFlushs[1].setTextColor(Color.RED);
			} else {
				textFlushs[1].setTextColor(Color.BLACK);
			}
			textFlushs[1].setText(flush.get(aux));
			break;
		case (R.id.buttonLess31):
			aux = value.indexOf(textValues[2].getText().toString()) - 1;
			if (aux == -1)
				aux = value.size() - 1;
			textValues[2].setText(value.get(aux));
			;
			break;

		case (R.id.buttonLess32):
			aux = flush.indexOf(textFlushs[2].getText().toString()) - 1;
			if (aux == -1)
				aux = flush.size() - 1;
			if (aux > 1) {
				textFlushs[2].setTextColor(Color.RED);
			} else {
				textFlushs[2].setTextColor(Color.BLACK);
			}
			textFlushs[2].setText(flush.get(aux));
			break;
		}
		checkValue();
	}

	public void checkValue() {
		if (textValues[0].getText().toString().equals("5")
				&& textFlushs[0].getText().toString().equals("\u2660")) {

			cardOne.setImageResource(R.drawable.espadas5);
		}
		if (textValues[1].getText().toString().equals("2")
				&& textFlushs[1].getText().toString().equals("\u2660")) {
			cardTwo.setImageResource(R.drawable.espadas2);
		}
		if (textValues[2].getText().toString().equals("5")
				&& textFlushs[2].getText().toString().equals("\u2663")) {
			cardThree.setImageResource(R.drawable.paus5);
		}

		if (cardOne.getId() == R.drawable.espadas5
				&& cardTwo.getId() == R.drawable.espadas2
				&& cardThree.getId() == R.drawable.paus5) {
			Intent intent = new Intent(getApplicationContext(), Level7.class);
			startActivity(intent);
		}

	}
}
